package com.hr.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.hr.model.UserModel;
import com.hr.service.UserService;

@Controller
public class LoginController {
	@Autowired
	private UserService userService;
	
	@RequestMapping(value="/checkLogin", method = RequestMethod.POST)
	public ModelAndView login(UserModel userModel,HttpServletRequest request,
			HttpServletResponse response) throws IOException{
		String message="";
		if("".equals(userModel.getLogin_name())||"".equals(userModel.getLogin_password())){
			message = "请输入用户名和密码！";
			return new ModelAndView("../../login").addObject("message", message);
		}
		String valifyCode = (String) request.getParameter("valifyCode");
		HttpSession session = request.getSession(true);
		if(valifyCode==null||(!valifyCode.equals(session.getAttribute("valifyCode")))){
			message = "验证码错误！";
			return new ModelAndView("../../login").addObject("message", message);
		}
		UserModel result = userService.checkLogin(userModel);
		if(result==null){
			message = "用户名或密码错误！";
			return new ModelAndView("../../login").addObject("message", message);
		}
		/*if("0".equals(result.getValidInd())){
			message = "此账号已被锁定，请联系管理员！";
			return new ModelAndView("../../login").addObject("message", message);
		}*/
		session.setAttribute("user", result);
		response.sendRedirect("index.html");
		return null;
	}
	/**
	 * 注销
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="/logout")
	public ModelAndView logout(HttpServletRequest request,
			HttpServletResponse response){
		request.getSession(true).invalidate();
		return new ModelAndView("../../login");
	}
}
